package com.atguigu.juc.t7;

public class Printer {
    public Integer num = 0;

    public synchronized void print0() {
        //等于0就不工作，因为这里要--
        while (num == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        //直行到这里num--
        num--;
        System.out.println(Thread.currentThread().getName() + "开始打印数字" + num);

        //唤醒其他线程
        this.notifyAll();
    }


    public synchronized void print1() {
        //等于1就不工作，因为这里要++
        while (num == 1) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        //直行到这里num++
        num++;
        System.out.println(Thread.currentThread().getName() + "开始打印数字" + num);

        //唤醒其他线程
        this.notifyAll();
    }
}

